﻿using FormView.UI;
using FormView.UI.AlertView;
using FormView.UI.ChoiceView;
using FormView.UI.PromptView;
using FxSuperCore.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormView
{
	public static class FormViewUtil
	{
		private static readonly string FormViewUtilNameSpace = "__FormViewUtilNameSpace__";

		public static void Toast(string msg, int timeout = 3000)
		{
			CommonUtils.ShowWinToast("消息提示", msg, ToolTipIcon.Info, timeout);
		}

		public static async Task<AlertResult> Alert(string msg, string positive = "确定", string negative = null, string neutral = null)
		{
			AlertFormWindow alertFormWindow = new AlertFormWindow(FormViewUtilNameSpace);
			alertFormWindow.LoadView(msg, positive, negative, neutral);
			alertFormWindow.ShowDialog();
			FormResult result = await alertFormWindow.WaitForResult();
			return (AlertResult)result.CastToInt();
		}

		public static async Task<int?> Choice(string[] choice, int defaultIdx = -1, string title = "请选择一项")
		{
			AssertArgument(choice, title);
			SingleChoiceWindow choiceWindow = new SingleChoiceWindow(FormViewUtilNameSpace);
			choiceWindow.Title = title;
			choiceWindow.LoadView(choice, defaultIdx);
			choiceWindow.ShowDialog();
			FormResult result = await choiceWindow.WaitForResult();
			if (!result.IsSuccess())
			{
				return null;
			}
			return result.CastToInt();
		}

		public static async Task<bool[]> MultiChoice(string[] items, int[] selection = null, string title = "请选择一项或多项")
		{
			AssertArgument(items, title);

			MultiChoiceWindow multiChoiceWindow = new MultiChoiceWindow(FormViewUtilNameSpace);
			multiChoiceWindow.Title = title;
			multiChoiceWindow.LoadView(items, selection == null ? new int[0] : new HashSet<int>(selection).ToArray());
			multiChoiceWindow.ShowDialog();
			FormResult result = await multiChoiceWindow.WaitForResult();
			if (!result.IsSuccess())
			{
				return null;
			}
			bool[] res = new bool[items.Length];
			result.CastToList().ForEach(formRes => res[formRes.CastToInt()] = true);
			return res;
		}

		public static async Task<object[]> Prompt(PromptDataView[] promptDatas, string title = "请填入以下内容")
		{
			AssertArgument(promptDatas, title);

			PromptFormWindow promptFormWindow = new PromptFormWindow(FormViewUtilNameSpace);
			promptFormWindow.Title = title;
			promptFormWindow.LoadView(new LinkedList<PromptDataView>(promptDatas));
			promptFormWindow.ShowDialog();

			FormResult result = await promptFormWindow.WaitForResult();
			if (!result.IsSuccess())
			{
				return null;
			}

			object[] res = new object[promptDatas.Length];
			List<FormResult> formResults = result.CastToList();
			for (var i = 0; i < formResults.Count; i++)
			{
				res[i] = formResults[i].Result;
			}

			return res;
		}

		private static void AssertArgument(params object[] args)
		{
			foreach (object arg in args)
			{
				if (arg == null)
				{
					throw new NullReferenceException("参数不能为NULL");
				}

				if (arg.GetType().IsArray)
				{
					var len = (int)arg.GetType().GetProperty("Length").GetValue(arg);
					if (len == 0)
					{
						throw new ArgumentException("数组长度不能为0");
					}
				}
			}
		}
	}
}
